#ifndef _TESTS_LIB_H
#define _TESTS_LIB_H

#include <unistd.h>
#include <stdio.h>
#include <time.h>

#include "cJSON.h"

char* record_process_rss(const char* suffix);
int32_t print_json(cJSON* root);

static const char* correction_value()
{
	static char buffer[1024];
	sprintf(buffer, "0000:00:05.0,desc,xxxx1\n0000:00:05.0,type_desc,dddd");
	return buffer;
}

static void clear_screen() {
    printf("\033[2J");
    printf("\033[H");
    fflush(stdout);
}

#define TYPE_JSON 1
#define TYPE_TEXT 2
#define USE_NODE_MONITOR_IMPL(type, func_prefix, mod, name, count, ignore, corr) \
void func_prefix_##mod() \
{ \
        char *p; \
        FILE* log = fopen("./log.txt", "w"); \
        if (log == NULL) { \
                printf("open log file failed\n"); \
                return; \
        } \
        cJSON* root = NULL; \
	if (type == TYPE_JSON) { \
		root = cJSON_CreateObject(); \
	} \
        ADD_MOD_2(mod,  name, corr,   ignore); \
        INIT_MODE(mod); \
        int n = count; \
        int k = 10; \
        int32_t size = 0; \
        char buff[16]; \
	char buffer[3072]; \
        for (int i = 0;i < n; i++) { \
		if (type == TYPE_JSON) { \
			UPDATE_MOD_JSON(mod, root); \
			size = print_json(root); \
		} else if (type == TYPE_TEXT) { \
			UPDATE_MOD_TEXT(mod, buffer, 3072); \
			size = printf("%s\n", buffer); \
		} \
                if (i % k == 0) { \
                        sprintf(buff, "%d", size); \
                        p = record_process_rss(buff); \
                        fputs(p, log); \
                } \
                sleep(1); \
        } \
        fclose(log); \
        CLENA_MOD(mod); \
	if (root) { \
		cJSON_Delete(root); \
	} \
}

//#define USE_NODE_MONITOR(mod, name, count, ignore, corr) \
	USE_NODE_MONITOR_IMPL(1, use_node_monitor, mod, name, count, ignore, corr)

//#define USE_NODE_MONITOR_2s(mod, name, count, ignore, corr) \
	USE_NODE_MONITOR_IMPL(2, use_node_monitor_text, mod, name, count, ignore, corr)

#define USE_NODE_MONITOR(mod, name, count, ignore, corr) \
void use_node_monitor_##mod() \
{ \
	char *p; \
	FILE* log = fopen("./log.txt", "w"); \
	if (log == NULL) { \
		printf("open log file failed\n"); \
		return; \
	} \
	cJSON* root = cJSON_CreateObject(); \
	ADD_MOD_2(mod,  name, corr,   ignore); \
	INIT_MODE(mod); \
	int n = count; \
	int k = 10; \
	int32_t size = 0; \
        char buff[16]; \
	for (int i = 0;i < n; i++) { \
		UPDATE_MOD_JSON(mod, root); \
		size = print_json(root); \
		if (i % k == 0) { \
			sprintf(buff, "%d", size); \
                        p = record_process_rss(buff); \
			fputs(p, log); \
		} \
		sleep(1); \
	} \
	fclose(log); \
	CLENA_MOD(mod); \
	cJSON_Delete(root); \
}

#define USE_NODE_MONITOR_2(mod, name, count, ignore, corr) \
static void use_node_monitor_text_##mod() \
{ \
	char *p; \
	FILE* log = fopen("./log.txt", "w"); \
	if (log == NULL) { \
		printf("open log file failed\n"); \
		return; \
	} \
	ADD_MOD_2(mod,  name, corr,   ignore); \
	INIT_MODE(mod); \
	int n = count; \
	int k = 10; \
	char buffer[3072]; \
	int32_t size = 0; \
        char buff[16]; \
	for (int i = 0;i < n; i++) { \
		UPDATE_MOD_TEXT(mod, buffer, 3072); \
		size = printf("%s\n", buffer);\
		if (i % k == 0) { \
			sprintf(buff, "%d", size); \
                        p = record_process_rss(buff); \
			fputs(p, log); \
		} \
		sleep(1); \
	} \
	fclose(log); \
	CLENA_MOD(mod); \
}

struct CtrlParam
{
	int type;
	int module;
	int count;
	int record_interval;
	char log[512];
	int err;
};

#endif
